package com.manage.modules.controller;

import java.util.List;
import java.util.Arrays;

import com.alibaba.fastjson.JSONObject;
import com.manage.modules.dto.TBaseFaultSelectDto;
import com.manage.modules.utils.DateUtil;
import com.manage.modules.utils.RandomUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.manage.common.annotation.Log;
import com.manage.common.core.controller.BaseController;
import com.manage.common.core.domain.AjaxResult;
import com.manage.common.enums.BusinessType;
import com.manage.modules.vo.TBaseFaultVo;
import com.manage.modules.bo.TBaseFaultQueryBo;
import com.manage.modules.bo.TBaseFaultAddBo;
import com.manage.modules.bo.TBaseFaultEditBo;
import com.manage.modules.service.ITBaseFaultService;
import com.manage.common.utils.poi.ExcelUtil;
import com.manage.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 故障信息Controller
 *
 * @author wx
 * @date 2021-08-18
 */
@Api(value = "故障信息控制器", tags = {"故障信息管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/modules/fault")
public class TBaseFaultController extends BaseController {

    private final ITBaseFaultService iTBaseFaultService;

    @GetMapping("/selectByDto")
    public AjaxResult selectByDto(TBaseFaultSelectDto selectDto){
        return AjaxResult.success(iTBaseFaultService.queryBySelectDto(selectDto));
    }

    /**
     * 获取故障单编号
     */
    @GetMapping("/getFaultCode")
    public AjaxResult getFaultCode() {
        JSONObject data = new JSONObject();
        data.put("faultCode", "fault_" + DateUtil.getNowDate() + RandomUtil.getRandomIntInRange(10000, 99999));
        return AjaxResult.success(data);
    }

    /**
     * 查询故障信息列表
     */
    @ApiOperation("查询故障信息列表")
    @PreAuthorize("@ss.hasPermi('modules:fault:list')")
    @GetMapping("/list")
    public TableDataInfo<TBaseFaultVo> list(TBaseFaultQueryBo bo) {
        startPage();
        List<TBaseFaultVo> list = iTBaseFaultService.queryList(bo);
        return getDataTable(list);
    }

    /**
     * 导出故障信息列表
     */
    @ApiOperation("导出故障信息列表")
    @PreAuthorize("@ss.hasPermi('modules:fault:export')")
    @Log(title = "故障信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<TBaseFaultVo> export(TBaseFaultQueryBo bo) {
        List<TBaseFaultVo> list = iTBaseFaultService.queryList(bo);
        ExcelUtil<TBaseFaultVo> util = new ExcelUtil<TBaseFaultVo>(TBaseFaultVo.class);
        return util.exportExcel(list, "故障信息");
    }

    /**
     * 获取故障信息详细信息
     */
    @ApiOperation("获取故障信息详细信息")
    @PreAuthorize("@ss.hasPermi('modules:fault:query')")
    @GetMapping("/{faultId}")
    public AjaxResult<TBaseFaultVo> getInfo(@PathVariable("faultId") Long faultId) {
        return AjaxResult.success(iTBaseFaultService.queryById(faultId));
    }

    /**
     * 新增故障信息
     */
    @ApiOperation("新增故障信息")
    @PreAuthorize("@ss.hasPermi('modules:fault:add')")
    @Log(title = "故障信息", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Void> add(@RequestBody TBaseFaultAddBo bo) {
        return toAjax(iTBaseFaultService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改故障信息
     */
    @ApiOperation("修改故障信息")
    @PreAuthorize("@ss.hasPermi('modules:fault:edit')")
    @Log(title = "故障信息", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult<Void> edit(@RequestBody TBaseFaultEditBo bo) {
        return toAjax(iTBaseFaultService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除故障信息
     */
    @ApiOperation("删除故障信息")
    @PreAuthorize("@ss.hasPermi('modules:fault:remove')")
    @Log(title = "故障信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{faultIds}")
    public AjaxResult<Void> remove(@PathVariable Long[] faultIds) {
        return toAjax(iTBaseFaultService.deleteWithValidByIds(Arrays.asList(faultIds), true) ? 1 : 0);
    }
}
